﻿using System;
using System.Drawing;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Lib.CommonForm
{
    public partial class frmRichTextBox_Log : FormBase
    {
        public frmRichTextBox_Log()
        {
            InitializeComponent();
        }
        public void Show_Form(String v_Log)
        {
            rtbDetailOfPrice.AppendText(LogBuilder(v_Log));
            this.ShowDialog();
        }
        string LogBuilder(string input_string)
        {
            StringBuilder sb = new StringBuilder();
            StringReader sr = new StringReader(input_string);
            int indent = 0;
            string s;
            while ((s = sr.ReadLine()) != null)
            {
                s = s.Trim();
                while (s.IndexOf("  ") >= 0)
                    s = s.Replace("  ", " ");
                if (s.StartsWith("</") && indent > 0)
                {
                    indent--;
                    s = string.Empty.PadRight(indent, '\t') + s;
                }
                else if (s.StartsWith("<") && indent >= 0)
                {
                    s = string.Empty.PadRight(indent, '\t') + s;
                    indent++;
                }
                else
                    s = string.Empty.PadRight(indent, '\t') + s;
                if (s.Length > 0)
                    sb.AppendLine(s);
            }
            sr.Close();
            return sb.ToString();
        }
        private void rtbDetailOfPrice_TextChanged(object sender, EventArgs e)
        {
            Font regular = new Font(rtbDetailOfPrice.Font, FontStyle.Regular);
            Font bold10 = new Font(rtbDetailOfPrice.Font.FontFamily, 10f, FontStyle.Bold);
            Font boldr = new Font(rtbDetailOfPrice.Font, FontStyle.Bold);
            Font italic = new Font(rtbDetailOfPrice.Font, FontStyle.Italic);
            rtbDetailOfPrice.Font = regular;
            Regex rg = new Regex(@"^.[^:]*.:\s*([\d.,*:/=\+\-% ∞]+(%|₫|KG)*)$", RegexOptions.Multiline);
            MatchCollection mc = rg.Matches(rtbDetailOfPrice.Text);
            foreach (Match m in mc)
            {
                rtbDetailOfPrice.SelectionStart = m.Groups[1].Index;
                rtbDetailOfPrice.SelectionLength = m.Groups[1].Length;
                rtbDetailOfPrice.SelectionColor = Color.Blue;
            }
            rg = new Regex(@"</*.+>", RegexOptions.Multiline);
            mc = rg.Matches(rtbDetailOfPrice.Text);
            foreach (Match m in mc)
            {
                rtbDetailOfPrice.SelectionStart = m.Groups[0].Index;
                rtbDetailOfPrice.SelectionLength = m.Groups[0].Length;
                if (m.Value.StartsWith("<Từ") || m.Value.StartsWith("</Từ"))
                    rtbDetailOfPrice.SelectionFont = boldr;
                else
                    rtbDetailOfPrice.SelectionFont = bold10;
            }
            rg = new Regex(@"^.*(Bước).*$", RegexOptions.Multiline);
            mc = rg.Matches(rtbDetailOfPrice.Text);
            foreach (Match m in mc)
            {
                rtbDetailOfPrice.SelectionStart = m.Groups[0].Index;
                rtbDetailOfPrice.SelectionLength = m.Groups[0].Length;
                rtbDetailOfPrice.SelectionColor = Color.Red;
                rtbDetailOfPrice.SelectionFont = regular;
                //rtbDetailOfPrice.SelectionFont = bold;
            }
            rg = new Regex(@"\(.+\)", RegexOptions.Multiline);
            mc = rg.Matches(rtbDetailOfPrice.Text);
            foreach (Match m in mc)
            {
                rtbDetailOfPrice.SelectionStart = m.Groups[0].Index;
                rtbDetailOfPrice.SelectionLength = m.Groups[0].Length;
                rtbDetailOfPrice.SelectionFont = new Font(rtbDetailOfPrice.SelectionFont, FontStyle.Italic | rtbDetailOfPrice.SelectionFont.Style);
            }
            rtbDetailOfPrice.SelectionLength = 0;
            rtbDetailOfPrice.SelectionStart = 0;
        }
    }
}
